// pages/usercenter/addressnew/addressnew.js
var utils = require('../../../utils/util');

Page({
  gotoPage: utils.gotoPage,

  /**
   * 页面的初始数据
   */
  data: {
    customItem: '全部',
    address: {
      region: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //addressid then edit else new
    console.log('addressid', options.id);

    var openid = wx.getStorageSync('openid');
    var HOST = getApp().globalData.HOST;
    var that = this;

    // edit address
    var addressid = options.id;
    if (addressid) {
      // get address info
      wx.request({
        url: HOST + 'user/address',
        data: {
          ACTION: 'ONE',
          id: addressid,
          openid: openid
        },
        success: function(resp) {
          console.log('address id:', resp)
          that.setData({
            address: {
              id: resp.data.data.id,
              fullname: resp.data.data.fullname,
              mobile: resp.data.data.mobile,
              region: resp.data.data.region.split(' '),
              detail: resp.data.data.detail,
              zipcode: resp.data.data.zipcode
            }
          });
        }
      })
    }
  },

  // 表单输入的双向绑定
  inputChange: function(event) {
    var key = event.currentTarget.dataset.key;
    this.data.address[key] = event.detail.value;
    this.setData({
      address: this.data.address
    });
    console.log(this.data.address)
  },

  // 保存地址
  sageAddress: function(event) {
    var openid = wx.getStorageSync('openid');
    var HOST = getApp().globalData.HOST;
    var that = this;
    var address = that.data.address;

    if (address.fullname == '' || address.mobile == '' || address.region == '' || address.detail == '') {
      wx.showToast({
        title: '请先填写地址信息',
        icon: 'none'
      })
      return false;
    }

    // 保存地址
    wx.request({
      url: HOST + 'user/address',
      method: (address.id ? 'put' : 'post'),
      data: {
        openid: openid,
        id: address.id,
        fullname: address.fullname,
        mobile: address.mobile,
        region: address.region.join(' '),
        detail: address.detail,
        zipcode: address.zipcode
      },
      success: function(resp) {
        console.log(resp);
        // 返回上一页
        wx.navigateBack({});
      },
      fail: function(resp) {
        console.log(resp);
      }
    })

  },

  deleteAddress: function(event) {
    var openid = wx.getStorageSync('openid');
    var HOST = getApp().globalData.HOST;
    var that = this;

    wx.showModal({
      title: '提示',
      content: '确认要删除吗?',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var id = that.data.address.id;
          wx.request({
            url: HOST + 'user/address',
            method: 'delete',
            data: {
              openid: openid,
              id: id
            },
            success: function(resp) {
              console.log('delete address: ', resp);
              wx.navigateBack({});
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },

  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'address.region': e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '国学小书童',
      path: '/pages/index/index',
      imageUrl: '/images/g1.jpg',
    }
  }
})